<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2017/6/22
  Time: 10:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!-- 引入JQuery -->
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.min.js"></script>
    <!-- 引入EasyUI -->
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/jquery.easyui.min.js"></script>
    <!-- 引入EasyUI的中文国际化js，让EasyUI支持中文 -->
    <script type="text/javascript" src="${pageContext.request.contextPath}/easyui/easyui-lang-zh_CN.js"></script>
    <!--
    <script  type="text/javascript" src="scripts/index.js"></script>
  -->
    <!-- 引入EasyUI的样式文件-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/easyui/themes/default/easyui.css" type="text/css"/>
    <!-- 引入EasyUI的图标样式文件-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/easyui/themes/icon.css" type="text/css"/>
</head>
<body>
<!--
    private Long id;
    private Long goodsId;
    private String goodsName;
    private Long storeId;
    private String storeName;
    private Long Quantity;
    private Date Date;
-->
<table id="group_list_dg" style="width:auto"></table>
</body>

<div id="btn">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="tb1_1.add()">新增</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="tb1_1.edit()">编辑</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="tb1_1.remove()">删除</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-save" plain="true" onclick="tb1_1.save()" style="display:none;"
       id="save1_1">保存</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-redo" plain="true" onclick="tb1_1.redo()" style="display:none;"
       id="redo1_1">取消编辑</a>
</div>

<script>
    var UrlConfig = {
        SysGroupList: '<%=request.getContextPath() %>/goodsinstore/list',
        SysGroupAdd: '<%=request.getContextPath() %>/goods/add',
        SysGroupUpdate: '<%=request.getContextPath() %>/goods/list/',
        SysGroupDelete: '<%=request.getContextPath() %>/goods/delete'
    };
    $(function () {
        $('#group_list_dg').datagrid({
            url: UrlConfig.SysGroupList,
            toolbar: '#btn',
            queryParams: {
                // start:0,
                // length:5
            },
            title: '商品入库详情列表',
            fitColumns: true,
            columns: [[
                {
                    field: 'id',
                    title: '编号',
                    width: 60,
                },
                {
                    field: 'goodsName',//field字段必须，为空也得写上，否则宽度为5px
                    title: '商品名称',
                    width: 100,
                    editor: {
                        type: 'combobox',
                        options: {
                            url: "${pageContext.request.contextPath}/goods/simplelist",
                            valueField: 'id',
                            textField: 'text',
                            panelHeight: '150',
                            editable: false,
                            required: true,
                        }
                    },

                },
                {
                    field: 'storeName',
                    title: '仓库名称',
                    width: 50,
                    editor: {
                        type: 'combobox',
                        options: {
                            url: "${pageContext.request.contextPath}/store/simplelist",
                            valueField: 'id',
                            textField: 'text',
                            panelHeight: '60',
                            editable: false,
                            required: true,
                        }
                    },
                },
                {
                    field: 'quantity',
                    title: '入库数量',
                    width: 50,
                    editor: {
                        type: 'validatebox',
                        editor: {
                            type: 'numberbox',
                            options: {
                                required: true,
                            }
                        },
                    },
                },
                {
                    field: 'date',
                    title: '商品入库日期',
                    width: 100,
                    editor: 'datebox'
                },
            ]],
            pagination: true,
            pageSize: 5,
            pageList: [5, 10, 15],
            pageNumber: 1,


            //在endEdit后自动触发检验是否符合validate才会执行
            onAfterEdit: function (rowIndex, rowData, changes) {
                $('#save1_1,#redo1_1').hide();
                var inserted = $('#group_list_dg').datagrid('getChanges', 'inserted');
                var updated = $('#group_list_dg').datagrid('getChanges', 'updated');
                console.log(inserted);
                if (inserted.length > 0) {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/goodsinstore/add ',
                        type: 'POST',
                        dataType: 'JSON',
                        contentType: "application/json",
                        data: JSON.stringify({
                            goodsId: inserted[0].goodsName,
                            storeId: inserted[0].storeName,
                            Quantity: inserted[0].quantity,
                            Date: inserted[0].date
                        }),
                    })
                        .done(function (data) {
                            if (data.status == 200) {
                                $('#group_list_dg').datagrid('loaded');
                                $.messager.show({
                                    title: '提示',
                                    msg: "您已成功添加",
                                });
                                tb1_1.editRow = undefined;
                            } else {
                                $.messager.alert("警告，添加失败！", "", data.message);
                            }
                        });
                }

                /*
                 //编辑部门信息
                 if (updated.length > 0) {
                 $.ajax({
                 url: host + 'status/set',
                 type: 'POST',
                 dataType: 'json',
                 data: {
                 id: updated[0].id,
                 name: updated[0].name,
                 },
                 })
                 .done(function (data) {
                 if (data.status == 200) {
                 $('#dg4_1').datagrid('loaded');
                 $('#dg4_1').datagrid('load');
                 $.messager.show({
                 title: '提示',
                 msg: "您已成功修改",
                 });
                 tb4_1.editRow = undefined;
                 } else if (data.status == 501) {
                 $.messager.alert("警告，修改失败！", "该状态id不存在", 'warning');
                 }
                 })
                 .fail(function () {
                 $('#dg4_1').datagrid('reload');
                 $.messager.alert("警告，修改失败！", "不明错误", 'warning');
                 })
                 .always(function () {

                 });

                 }
                 $('#dg4_1').datagrid('unselectAll');
                 $("#dg4_1").datagrid("showColumn", "id");
                 tb4_1.editRow = undefined;
                 */
            }//end  onAfterEdit:
        });
    });
    tb1_1 = {
        editRow: undefined,
        add: function () {
            console.log("save1_1");
            $('#save1_1,#redo1_1').show();
            if (tb1_1.editRow == undefined) {
                $('#group_list_dg').datagrid('insertRow', {
                    index: 0,
                    row: {}
                });
                $('#group_list_dg').datagrid('beginEdit', 0);
                tb1_1.editRow = 0;
                $('#group_list_dg').datagrid('hideColumn', 'role');
                $('#group_list_dg').datagrid('removeEditor', 'role');
            }
        },
        redo: function () {
            $('#save1_1,#redo1_1').hide();
            tb1_1.editRow = undefined;
            $('#group_list_dg').datagrid('rejectChanges');
        },
        save: function () {
            $('#group_list_dg').datagrid('endEdit', tb1_1.editRow);
            tb1_1.editRow = undefined;
        },
    }

</script>
</html>
